package com.leetcode.code;

/**
 * 已发
 */
public class LeetCode0121Java {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.maxProfit(new int[] { 7, 1, 5, 3, 9, 4 }));
    }

    static class Solution {
        public int maxProfit(int[] prices) {
            int minprice = Integer.MAX_VALUE;
            int maxprofit = 0;

            for (int index = 0; index < prices.length; index++) {
                if (prices[index] < minprice) {
                    minprice = prices[index];
                } else if (prices[index] - minprice > maxprofit) {
                    maxprofit = prices[index] - minprice;
                }
            }

            return maxprofit;
        }
    }
}
